package light;

import javax.vecmath.Point3d;
import javax.vecmath.Vector3d;
import static helper.Util.getVector;
import static rayTracer.ApplicationInterface.*;

public class PointLight extends Light {
	
	Point3d position;
	// Propiedades de la funcion de falloff, se pueden modificar manualmente.
	// Si se modifica distanceFalloffEnd no olvidar modificar maxDistance.
	float distanceFalloffStart = DISTANCE_FALLOFF_START;
	float distanceFalloffEnd = DISTANCE_FALLOFF_END;
	
	public PointLight(Point3d position) {
		this.position = position;
		this.maxDistance = distanceFalloffEnd;
	}
	
	public PointLight(double x, double y, double z) {
		this.position = new Point3d(x,y,z);
		this.maxDistance = distanceFalloffEnd;
	}
	
	public PointLight(Point3d position, float distanceFalloffStart, float distanceFalloffEnd) {
		this.position = position;
		this.distanceFalloffStart = distanceFalloffStart;
		this.distanceFalloffEnd = distanceFalloffEnd;
		this.maxDistance = distanceFalloffEnd;
	}
	
	public PointLight(Point3d position, float[] color, float distanceFalloffStart,
			float distanceFalloffEnd, float iD, float iS) {
		this.position = position;
		this.color = color;
		this.distanceFalloffStart = distanceFalloffStart;
		this.distanceFalloffEnd = distanceFalloffEnd;
		this.maxDistance = distanceFalloffEnd;
		this.iD = iD;
		this.iS = iS;
	}

	// Devuelve un vector que apunta desde el punto hacia la posicion de la luz
	public Vector3d getLightDir(Point3d p) {
		return getVector(p, position);
	}
	
	// Devuelve la distancia desde la luz al punto p
	public double getdistanceToLight(Point3d p) {
		return getVector(p, position).length();
	}

	// Funcion de falloff lineal
	// Calcula la iluminacion de un punto respecto a la posicion de la luz puntual
	public double falloff(Point3d p) {
		double distance = getVector(p, position).length();
		if( 0.001 <= distance && distance <= distanceFalloffStart ) {
			return 1;
		}
		if( distanceFalloffStart < distance && distance < distanceFalloffEnd ) {
			return (distanceFalloffEnd - distance)/(distanceFalloffEnd - distanceFalloffStart);
		}
		// distance >= distanceFalloffEnd
		return 0;
	}

	// No falloff
	/*
	public double falloff(Point3d p) {
		return 1;
	}
	*/
	@Override
	public String toString() {
		// TODO Auto-generated method stub
		return null;
	}

}
